home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
public
/
fax
/
src
/
port
/
hpux
/
flock.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
2KB
|
78 lines
/*
* flock (fd, operation)
*
* This routine performs some file locking like the BSD 'flock'
* on the object described by the int file descriptor 'fd',
* which must already be open.
*
* The operations that are available are:
*
* LOCK_SH - get a shared lock.
* LOCK_EX - get an exclusive lock.
* LOCK_NB - don't block (must be ORed with LOCK_SH or LOCK_EX).
* LOCK_UN - release a lock.
*
* Return value: 0 if lock successful, -1 if failed.
*
* Note that whether the locks are enforced or advisory is
* controlled by the presence or absence of the SETGID bit on
* the executable.
*
* Note that there is no difference between shared and exclusive
* locks, since the 'lockf' system call in SYSV doesn't make any
* distinction.
*
* The file "<sys/file.h>" should be modified to contain the definitions
* of the available operations, which must be added manually (see below
* for the values).
*
* This comes from a regular post in comp.sys.hp. /lars-owe
*/
#include <unistd.h>
#include <sys/file.h>
#include <errno.h>
#include "port.h"
extern int errno;
int
flock (int fd, int operation)
{
int i;
switch (operation) {
/* LOCK_SH - get a shared lock */
case LOCK_SH:
/* LOCK_EX - get an exclusive lock */
case LOCK_EX:
i = lockf (fd, F_LOCK, 0);
break;
/* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
case LOCK_SH|LOCK_NB:
/* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
case LOCK_EX|LOCK_NB:
i = lockf (fd, F_TLOCK, 0);
if (i == -1)
if ((errno == EAGAIN) || (errno == EACCES))
errno = EWOULDBLOCK;
break;
/* LOCK_UN - unlock */
case LOCK_UN:
i = lockf (fd, F_ULOCK, 0);
break;
/* Default - can't decipher operation */
default:
i = -1;
errno = EINVAL;
break;
}
return (i);
}